package com.juc.semaphore.semaphore8;

import java.util.concurrent.Semaphore;

public class MyService {
    /**
     * drainPermits() 返回许可可用的数量 并将许可可用数量清0
     */
    private Semaphore semaphore = new Semaphore(10, true);

    public void testMethod() {
        try {
            semaphore.acquire();
            System.out.println(semaphore.availablePermits());
            System.out.println(semaphore.drainPermits() + "  " + semaphore.availablePermits());
            System.out.println(semaphore.drainPermits() + "  " + semaphore.availablePermits());
            System.out.println(semaphore.drainPermits() + "  " + semaphore.availablePermits());
        } catch (InterruptedException e) {
            e.printStackTrace();
        } finally {
            semaphore.release();
        }
    }

}
